package fy.erp.reports;

import java.io.OutputStream;
import java.util.List;

import fy.erp.bean.ProjectCostBean;


import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class ReportsCreator {

	private OutputStream outputStream;

	private List<ProjectCostBean> rowBeans;

	public void setRowBeans(List<ProjectCostBean> rowBeans) {
		this.rowBeans = rowBeans;
	}

	public void setOutputStream(OutputStream outputStream) {
		this.outputStream = outputStream;
	}

	public boolean write() throws Exception {
		// if (parse()) {
		WritableWorkbook workbook = Workbook.createWorkbook(outputStream);
		try {
			WritableSheet sheet = workbook.createSheet("报表", 0);
			sheet.addCell(new Label(0, 0, "序号"));
			sheet.addCell(new Label(1, 0, "公司"));
			sheet.addCell(new Label(2, 0, "子目段"));
			sheet.addCell(new Label(3, 0, "项目名称"));
			sheet.addCell(new Label(4, 0, "项目令号"));
			sheet.addCell(new Label(5, 0, "上上月发生额"));
			sheet.addCell(new Label(6, 0, "上月发生额"));
			sheet.addCell(new Label(7, 0, "本月发生额"));
			sheet.addCell(new Label(8, 0, "累计发生额"));
			sheet.addCell(new Label(9, 0, "预算额"));
			sheet.addCell(new Label(10, 0, "本月余额"));
			sheet.addCell(new Label(11, 0, "完成率"));
			// if (result != null) {
			for (int i = 0, size = rowBeans.size(); i < size; i++) {
				ProjectCostBean bean = rowBeans.get(i);
				sheet.addCell(new Label(0, i + 1, String.valueOf(i + 1)));
				sheet.addCell(new Label(1, i + 1, bean.getCompany()));
				sheet.addCell(new Label(2, i + 1, bean.getSubject()));
				sheet.addCell(new Label(3, i + 1, bean.getProjectName()));
				sheet.addCell(new Label(4, i + 1, bean.getNumber()));
				sheet.addCell(new Label(5, i + 1, bean.getLastLastMonthCost()));
				sheet.addCell(new Label(6, i + 1, bean.getLastMonthCost()));
				sheet.addCell(new Label(7, i + 1, bean.getMonthCost()));
				sheet.addCell(new Label(8, i + 1, bean.getCost()));
				sheet.addCell(new Label(9, i + 1, bean.getYujifeiyong()));
				sheet.addCell(new Label(10, i + 1, bean.getBalance()));
				sheet.addCell(new Label(11, i + 1, bean.getPercentage() + "%"));
			}
			workbook.write();
			return true;
		} finally {
			try {
				if (workbook != null)
					workbook.close();
				outputStream.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		// }
		// return false;
	}

//	public static void main(String[] args) {
//		// System.out
//		// .println("1233.15255'".substring(0, "1233.15255".indexOf(".")));
//
//		System.out.println("1233.15255'".length());
//		System.out.println("1233.15255'".indexOf("'"));
//		// System.out.println("1233.15255".substring(
//		// "1233.15255".indexOf(".") + 1, "1233.15255".length()));
//	}
}

//class RowBean {
//	private String company;
//	private String projectName;
//	private String number;
//	private String subject;
//	private String cost;
//	private String yujifeiyong;
//
//	public String getCompany() {
//		return company;
//	}
//
//	public void setCompany(String company) {
//		this.company = company;
//	}
//
//	public String getProjectName() {
//		return projectName;
//	}
//
//	public void setProjectName(String projectName) {
//		this.projectName = projectName;
//	}
//
//	public String getNumber() {
//		return number;
//	}
//
//	public void setNumber(String number) {
//		this.number = number;
//	}
//
//	public String getSubject() {
//		return subject;
//	}
//
//	public void setSubject(String subject) {
//		this.subject = subject;
//	}
//
//	public String getCost() {
//		return cost;
//	}
//
//	public void setCost(String cost) {
//		this.cost = cost;
//	}
//
//	public String getYujifeiyong() {
//		return yujifeiyong;
//	}
//
//	public void setYujifeiyong(String yujifeiyong) {
//		this.yujifeiyong = yujifeiyong;
//	}
//
//}
